<?php
	check_login();
	$a = getAction();
		
	switch ($a) 
	{
		case "index":
			$url = "admin.php?m=user";
			$sql = " select * from " .table('user')
				 . " WHERE 1=1 "
				 . " order by id desc";
			
			//分页
			$rscount = $db->getOne("select count(1) from (" . $sql . ") temp");
			$page = max(1, intval($_GET['page']));
			$start = ($page-1) * PAGE_SIZE;
			$pagelist = multipage($rscount, PAGE_SIZE, $page, $url);
			$sql .= " limit $start, " . PAGE_SIZE;
			
			$smarty->assign("pagelist", $pagelist);
			
			$list = $db->getAll($sql);
			
			for($i = 0; $i < count($list); $i++) {
				$list[$i]["user_type_name"] = USER_TYPE::getTypeName($list[$i]["user_type"]);
			}
				
			$smarty->assign("list",$list);
			$smarty->display("user.html");

			break;
		
		case "export":
			$fileName = "申请用户信息" . date("Ymd") . ".csv";
			header('Content-Type: application/octet-stream');
			header('Content-Disposition: attachment; filename=' . $fileName);
			echo "姓名,业务类型,办理业务手机号,收取礼品手机号,收货地址,申请时间\r\n";
			
			$sql = "select  *  from " . table('user') . " order by id desc  ";
			$rscount = $db->getOne(" select count(1) from " .table('user'));

			$pagesize = 1000;
			$page = 1;
			
			for($i = 0; $i <= $rscount / $pagesize; $i++) {
				$start = ($page - 1) * $pagesize;
				$list = $db->getAll($sql . " limit $start,$pagesize");
				 
				foreach ($list as $item) {
					echo  $item["user_name"] . ","
						. USER_TYPE::getTypeName($item["user_type"]) . ","
						. $item["bm_phone"] . ","
						. $item["gift_phone"] . ","
						. $item["address"] . ","
						. $item["insert_time"] . "\r\n";
				}
			}
			break;
	}

?>

